package com.hundsun.yr.universal.p2p.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import com.hundsun.mobile.zwgt.R;
import com.hundsun.yr.universal.library.eventbus.EventCenter;
import com.hundsun.yr.universal.library.netstatus.NetUtils;
import com.hundsun.yr.universal.library.widget.HSButton;
import com.hundsun.yr.universal.p2p.ui.activity.base.BaseActivity;

/**
 * Created by xx on 2017/7/19.
 * 还款方式
 */
public class RepayMethodActivity extends BaseActivity implements CompoundButton.OnCheckedChangeListener, View.OnClickListener {
    private CheckBox rb_paymethod1;
    private CheckBox rb_paymethod2;
    private CheckBox rb_paymethod3;
    private CheckBox rb_paymethod4;
    private HSButton btn_sure;

    private String paymethod;
    private String paymethod1 = "";
    private String paymethod2 = "";
    private String paymethod3 = "";
    private String paymethod4 = "";

    private int SETTINGPAYMETHOD = 3;


    @Override
    protected boolean isApplyKitKatTranslucency() {
        return true;
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.act_repaymethod;
    }

    @Override
    protected void initViewsAndEvents() {
        findView();
        toolbar_title.setText(getString(R.string.repayment_method));

        String[] split = paymethod.split(",");
        for (String s : split) {
            if (TextUtils.equals(s, "1")) {
                rb_paymethod1.setChecked(true);
                paymethod1 = "1";
            } else if (TextUtils.equals(s, "2")) {
                rb_paymethod2.setChecked(true);
                paymethod2 = "2";
            } else if (TextUtils.equals(s, "3")) {
                rb_paymethod3.setChecked(true);
                paymethod3 = "3";
            } else if (TextUtils.equals(s, "4")) {
                rb_paymethod4.setChecked(true);
                paymethod4 = "4";
            }
        }

        rb_paymethod1.setOnCheckedChangeListener(this);
        rb_paymethod2.setOnCheckedChangeListener(this);
        rb_paymethod3.setOnCheckedChangeListener(this);
        rb_paymethod4.setOnCheckedChangeListener(this);
        btn_sure.setOnClickListener(this);
    }

    public void findView() {
        rb_paymethod1 = findView(R.id.rb_paymethod1);
        rb_paymethod2 = findView(R.id.rb_paymethod2);
        rb_paymethod3 = findView(R.id.rb_paymethod3);
        rb_paymethod4 = findView(R.id.rb_paymethod4);
        btn_sure = findView(R.id.btn_sure);
    }

    @Override
    protected void getBundleExtras(Bundle extras) {
        paymethod = extras.getString("paymethod");
    }

    @Override
    protected void onEveComming(EventCenter eventCenter) {

    }

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    protected View getLoadingTargetView() {
        return null;
    }

    @Override
    protected void onNetworkConnected(NetUtils.NetType type) {

    }

    @Override
    protected void onNetworkDisConnected() {

    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return true;
    }

    @Override
    protected boolean toggleOverridePendingTransition() {
        return false;
    }

    @Override
    protected TransitionMode getOverridePendingTransitionMode() {
        return null;
    }

    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean ischeck) {
        switch (compoundButton.getId()) {
            case R.id.rb_paymethod1:
                if (ischeck) {
                    paymethod1 = "1";
                } else {
                    paymethod1 = "";
                }
                break;
            case R.id.rb_paymethod2:
                if (ischeck) {
                    paymethod2 = "2";
                } else {
                    paymethod2 = "";
                }
                break;
            case R.id.rb_paymethod3:
                if (ischeck) {
                    paymethod3 = "3";
                } else {
                    paymethod3 = "";
                }
                break;
            case R.id.rb_paymethod4:
                if (ischeck) {
                    paymethod4 = "4";
                } else {
                    paymethod4 = "";
                }
                break;
        }
    }

    @Override
    public void onClick(View view) {
        if (!NetUtils.isNetworkConnected(mContext)){
            showToast(R.string.common_no_network);
            return;
        }

        switch (view.getId()) {
            case R.id.btn_sure:
                getPaymethod();
                if (paymethod.isEmpty()){
                    showToast("至少应该选择一种还款方式");
                    return;
                }

                Intent intent = getIntent();
                intent.putExtra("paymethod", paymethod);
                setResult(SETTINGPAYMETHOD, intent);
                finish();
                break;
        }
    }

    public String getPaymethod() {
        String[] a = {paymethod1, paymethod2, paymethod3, paymethod4};
        StringBuffer sp = new StringBuffer();
        for (String s : a) {
            if (!s.isEmpty()) {
                sp.append(s + ",");
            }
        }

        paymethod = sp + "";

        if (paymethod.endsWith(",")) {
            paymethod = paymethod.substring(0, paymethod.length() - 1);
        }
        return paymethod;
    }
}
